package com.charley.icloud.common.utils.security;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;

import com.charley.icloud.common.constants.core.SecurityConstants;
import com.charley.icloud.common.vo.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * JWT工具类
 */
@Slf4j
public class UserJwtUtil {

    /**
     * 获取用户地区信息
     */
    public static UserInfo getUserInfo() {
        JSONObject jwtPayload = JwtUtil.getJwtPayload();
        UserInfo userInfo = new UserInfo();
        if (jwtPayload != null) {
            userInfo.setUserId(getUserId().toString());
            userInfo.setUsername(getUsername());
            String areaCode = jwtPayload.getStr(SecurityConstants.AREA_CODE);
            userInfo.setAreaLevel(getSpecialLevel(areaCode, jwtPayload.getStr(SecurityConstants.AREA_LEVEL)));
            userInfo.setAreaCode(getSpecialCode(areaCode));
            userInfo.setAreaName(jwtPayload.getStr(SecurityConstants.AREA_NAME));
            userInfo.setParentAreaCode(jwtPayload.getStr(SecurityConstants.PARENT_AREA_CODE));
            userInfo.setGrantApi(jwtPayload.getStr(SecurityConstants.GRANT_API));
        }
        return userInfo;
    }

    /**
     * 解析JWT获取用户ID
     */
    public static Long getUserId() {
        Long userId = null;
        JSONObject jwtPayload = JwtUtil.getJwtPayload();
        if (jwtPayload != null) {
            userId = jwtPayload.getLong(SecurityConstants.USER_ID_KEY);
        }
        if (null == userId || userId.compareTo(0L) <= 0) {
            userId = jwtPayload.getLong("memberId");
        }
        return userId;
    }

    public static Long getMemberId() {
        Long memberId = null;
        JSONObject jwtPayload =  JwtUtil.getJwtPayload();
        if (jwtPayload != null) {
            memberId = jwtPayload.getLong("memberId");
        }
        return memberId;
    }

    /**
     * 解析JWT获取用户部门ID
     */
    public static Long getDeptId() {
        return JwtUtil.getJwtPayload().getLong("deptId");
    }

    /**
     * 解析JWT获取获取用户名
     */
    public static String getUsername() {
        return JwtUtil.getJwtPayload().getStr(SecurityConstants.USER_NAME_KEY);
    }

    /**
     * 获取应用标识
     */
    public static String getGrantApi() {
        return JwtUtil.getJwtPayload().getStr(SecurityConstants.GRANT_API);
    }

    /**
     * JWT获取用户角色列表
     *
     * @return 角色列表
     */
    public static List<String> getRoles() {
        List<String> roles;
        JSONObject payload = JwtUtil.getJwtPayload();
        if (payload.containsKey(SecurityConstants.JWT_AUTHORITIES_KEY)) {
            roles = payload.getJSONArray(SecurityConstants.JWT_AUTHORITIES_KEY).toList(String.class);
        } else {
            roles = Collections.emptyList();
        }
        return roles;
    }

    /**
     * 是否「超级管理员」
     */
    public static boolean isRoot() {
        List<String> roles = getRoles();
        return CollectionUtil.isNotEmpty(roles) && roles.contains("ROOT");
    }


    public static String getSpecialCode(String areaCode) {

        if (areaCode.equals("110000") || areaCode.equals("310000") || areaCode.equals("120000") || areaCode.equals("500000")) {
//            return areaCode + 100;
            return areaCode.substring(0, 3) + 100;
        }
        return areaCode;
    }

    public static Integer getSpecialLevel(String areaCode, String level) {

        if (areaCode.equals("110000") || areaCode.equals("310000") || areaCode.equals("120000") || areaCode.equals("500000")) {
            return 3;
        }
        return Integer.valueOf(level);
    }

    public static boolean userPermissionVerification(String verifyAreaCode) {

        UserInfo userInfo = getUserInfo();
        int level = userInfo.getAreaLevel();
        String code = userInfo.getAreaCode();
        String dealCode = code.substring(0,level+1);
        return level == 0 || null == verifyAreaCode || StringUtils.isBlank(verifyAreaCode) || verifyAreaCode.startsWith(dealCode);
    }
}
